package rsj.api.product;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.BoxUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.report.entity.ReportParam;
import rsj.api.user.entity.UserAccount;
import rsj.common.ProdStatusEnum;
import rsj.domain.ProductPageQuery;
import rsj.domain.ProductTypePo;
import rsj.domain.ProductTypeVo;
import rsj.service.ProductService;
import rsj.service.user.IUserService;
import rsj.util.PaginationData;

/**
 * @author yuanqinglong
 * @since 2020/10/29 16:53
 */
@RestController
public class GetProdByOrg {

    @Autowired
    private IUserService userService;

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "rsj_prod_list_by_org")
    public Box executeLink(Box in) {
        MapNode data = in.getMapNode("data");
        MapNode user = in.getMapNode("$user");
        UserAccount userInfo = userService.getUserInfo(user.getValue("User_Id"));
        data.putNode("Org_Id", userInfo.getUserOrgId());
        PaginationData<TableNode> paginationData;
        ProductPageQuery query = buildPageQuery(data);
        if (StringUtils.isBlank(data.getValue("Org_Id"))) {
            paginationData = productService.getDataMarketPage(query);
        } else {
            paginationData = productService.getPageByOrgid(query);
        }

        MapNode outData = new MapNode();
        outData.putNode("Prod_List", paginationData.getDataList().toJson());
        outData.putNode("Prod_Count", String.valueOf(paginationData.getTotalCount()));

        return BoxUtil.successBoxToData(in, "获取用户查询列表信息！", outData);
    }

    private ProductPageQuery buildPageQuery(MapNode data) {
        ProductPageQuery query = new ProductPageQuery();
        query.setIsProdQuery();// 设置是全量查询

        if (null == data) {
            return query;
        }

        query.setPageCount(data.getValueIntDef("Page_Count", 0));
        query.setPageSize(data.getValueIntDef("Page_Size", 0));
        query.setProdName(data.getValue("Query_Key"));
        query.setOrgId(data.getValue("Org_Id"));
        query.setProdType(data.getValue("Prod_Type"));
        query.setProdStatus("1");
        return query;
    }
}
